import { ref, computed, reactive } from 'vue'
import { defineStore } from 'pinia'
import { darkTheme, lightTheme } from 'naive-ui'
export const useMetaStore = defineStore('meta', () => {
	const metas = ref([])
	const checkKey = ref('')
	function push (meta) {
		for (const i in metas.value) {
			if (meta.key === metas.value[i].key) {
				checkKey.value = meta.key
				return
			}
		}
		metas.value.push(meta)
		checkKey.value = meta.key
	}
	function getMetas () {
		return metas.value
	}
	function del (index) {
		metas.value.splice(index, 1)
	}
	function changeKey (key) {
		checkKey.value = key
	}
	return { metas, push, getMetas, del, checkKey }
}, {
	persist: {
		storage: sessionStorage
	}
})

export const useLoadingStore = defineStore('loading', () => {
	const loading = ref(0)
	function openLoading() {
		loading.value++
	}
	function closeLoading() {
		loading.value--
	}
	function getLoading() {
		return loading.value
	}
	return { loading, openLoading, closeLoading, getLoading }
})

export const userTokenStore = defineStore('token', () => {
	const token = ref(null)
	function setToken (value) {
		token.value = value
	}
	function getToken () {
		return token.value
	}
	return { token, setToken, getToken }
}, {
		persist: {
			storage: sessionStorage
		}
	}
)
export const userAdminStore = defineStore('admin', () => {
	const admin = ref(null)
	function setAdmin (value) {
		admin.value = value
	}
	function getAdmin () {
		return admin.value
	}
	return { admin, setAdmin, getAdmin }
}, {
		persist: {
			storage: sessionStorage
		}
	}
)
export const useAiSessionStore = defineStore('aiSession', () => {
	const sessionId = ref(null)
	function setSessionId (value) {
		sessionId.value = value
	}
	function getSessionId () {
		return sessionId.value
	}
	return { sessionId, setSessionId, getSessionId }
}, {
	persist: {
		storage: sessionStorage
	}
})
export const useThemeStorage = defineStore('theme', () => {
	const theme = ref('light')
	const themeMap = {
		dark: darkTheme,
		light: lightTheme
	}
	function setTheme (value) {
		theme.value = value
	}
	function getTheme () {
		return themeMap[theme.value]
	}
	return { theme, setTheme, getTheme }	
}, {
	persist: {
		storage: localStorage
	}
})
export const useParamStorage = defineStore('param', () => {
	const params = ref({})
	function setParams (value) {
		params.value = value
	}
	function getParams () {
		return params.value
	}
	return { params, setParams, getParams }	
}, {
	persist: {
		storage: sessionStorage
	}
})